package de.unimannheim.ki.tests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;

import de.unimannheim.ki.databaseentities.MusicObject;
import de.unimannheim.ki.databaseentities.Musicuser;
import de.unimannheim.ki.usermanagement.DatabaseUserDAO;
import weka.core.Instance;
import weka.core.Instances;
import weka.experiment.InstanceQuery;


public class Wekatest {
	public static void main(String[] args){
		
		DatabaseUserDAO dao = DatabaseUserDAO.getDAO();
		File sfile = new File("sample4.csv");
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(sfile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<MusicObject> objects = dao.getAllObjects();
		
		// first write header line
	
		/*
		
		for (MusicObject object : objects) {
			if(first){
				theline = theline +  object.getId();
			}else {
				theline = theline +", " + object.getId();
			}
			first = false;
		}
		
		*/
		
		
		
		
		
		boolean first = true; 
		List<Musicuser> user = dao.getAllUsers();
		List<MusicObject> userobjects = null;
		int biggestsetsize = 0;
		for (Musicuser musicuser : user) {
			if ( musicuser.getObjects().size() > biggestsetsize) {
				biggestsetsize = musicuser.getObjects().size();
				
			}
		}
		
		System.out.println("biggest size;" + biggestsetsize);
		
		String line ="";
		
		first = true;
		for (int i = 0; i < biggestsetsize; i ++){
			if(first) {
				line = line + "itemno"+i;
			}else {
				line = line+ ","+"itemno"+i;
			}
			first = false;
		}
		
		pw.print(line +"\n");
		
		first = true;
		for (Musicuser musicuser : user) {
			line ="";
			userobjects = musicuser.getObjects();
			
			if (userobjects.size()== 0) continue;
				first = true;
			for(int i= 0; i < biggestsetsize; i++){
				
				if(i < userobjects.size()){
					if(first) {
						line = line +userobjects.get(i).getId();
					}else {
						line = line+ ","+userobjects.get(i).getId();
					}
					
				}else {
					if(first) {
						line = line +"?";
					}else {
						line = line+ ",?";
					}
				}
				first = false;
			}
			pw.print(line +"\n");
		}
		pw.flush();
		pw.close();
		
		/*
		for (Musicuser musicuser : user) {
			theline="";
			first = true;
			userobjects = musicuser.getObjects();
			//loop over all object 
			 objects = dao.getAllObjects();
			for (MusicObject musicObject : objects) {	
				if(! first) {
					if(userobjects.contains(musicObject)){
						theline = theline + ", " + "1";
					}else {
						theline = theline + ", " + "0";
					}
				} else {
					first = false;
					if(userobjects.contains(musicObject)){
						theline = theline + "1";
					}else {
						theline = theline + "0";
					}
				}
			}
			if (!theline.equals("")){
				
				pw.println(theline);
			}
		}
		*/
		
		 InstanceQuery query =  null;
		/*try {
			query = new InstanceQuery();
			 query.setUsername("test");
			 query.setPassword("testtest");
			 query.setQuery("select * from MusicObject m inner join likes l on m.ID = l.ObjectId inner join MUSICUSER mu ");
			 
			 Instances data = query.retrieveInstances();
			 
			Instance bla = new Instance(6);
			 
			System.out.println( data.numInstances());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
	}
}
